package com.ebeitech.doorapp.domain;

import com.ebeitech.doorapp.db.annotation.Column;
import com.ebeitech.doorapp.db.annotation.Id;
import com.ebeitech.doorapp.db.annotation.PrimaryKey;
import com.ebeitech.doorapp.db.annotation.Table;
import com.ebeitech.library.util.StringUtil;

import org.json.JSONObject;

import java.io.Serializable;

/**
 * Created by alice on 2018/4/26.
 */

@Table(name = "AppInfoTable")
public class AppInfo implements Serializable {

	public static final String APP_TYPE_ONLINE = "0";
	public static final String APP_TYPE_OFFLINE = "1";

	private int _id;

	private String appId;
	private String appName;
	/**
	 * App的Logo
	 */
	private String appLogo;
	/**
	 * APP类型，0是在线APP,1离线APP
	 */
	private String appType;
	/**
	 * APP的URL(在线APP就是在线路径，离线APP就是下载路径)
	 */
	private String appUrl;
	/**
	 * APP描述
	 */
	private String appDesc;
	private String groupId;
	private String groupName;
	private String version;
	/**
	 * APP更新时间
	 */
	private String updateTime;
	private String companyName;

	@Id
	@Column(name = "_id")
	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	@PrimaryKey
	@Column(name = "appId")
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	@Column(name = "appName")
	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	@Column(name = "appLogo")
	public String getAppLogo() {
		return appLogo;
	}

	public void setAppLogo(String appLogo) {
		this.appLogo = appLogo;
	}

	@Column(name = "appType")
	public String getAppType() {
		return appType;
	}

	public void setAppType(String appType) {
		this.appType = appType;
	}

	@Column(name = "appUrl")
	public String getAppUrl() {
		return appUrl;
	}

	public void setAppUrl(String appUrl) {
		this.appUrl = appUrl;
	}

	@Column(name = "appDesc")
	public String getAppDesc() {
		return appDesc;
	}

	public void setAppDesc(String appDesc) {
		this.appDesc = appDesc;
	}

	@Column(name = "groupId")
	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	@Column(name = "groupName")
	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	@Column(name = "version")
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	@Column(name = "updateTime")
	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	@Column(name = "companyName")
	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public void initWithJson(String jsonStr) {
		AppInfo appInfo = this;
		try {
			JSONObject jsonObject = new JSONObject(jsonStr);
			appInfo.setAppId(jsonObject.optString("id"));
			appInfo.setAppName(jsonObject.optString("name"));
			appInfo.setAppLogo(jsonObject.optString("logo"));
			appInfo.setAppType(jsonObject.optInt("type") + "");
			appInfo.setAppUrl(jsonObject.optString("url"));
			appInfo.setAppDesc(jsonObject.optString("appDesc"));
			String updateTime = jsonObject.optString("updateTime");
			if (StringUtil.isStringNullOrEmpty(updateTime)) {
				updateTime = jsonObject.optString("createTime");
			}
			appInfo.setUpdateTime(updateTime);
			JSONObject groupObject = jsonObject.optJSONObject("appGroup");
			if (groupObject != null) {
				appInfo.setGroupId(groupObject.optString("id"));
				appInfo.setGroupName(groupObject.optString("name"));
			}
			JSONObject appObject = jsonObject.optJSONObject("appPack");
			if (appObject != null) {
				appInfo.setVersion(appObject.optString("version"));
			}
			JSONObject companyObject = jsonObject.optJSONObject("company");
			if (companyObject != null) {
				appInfo.setCompanyName(companyObject.optString("name"));
			}
		} catch (Exception e) {}
	}
}
